package wallegame;

import static wallegame.Constants.BAD_PARAMS;
import static wallegame.Constants.FILE_FORMAT_ERROR;
import static wallegame.Constants.FILE_READ_ERROR;
import static wallegame.Constants.FILE_READ_ERROR2;
import static wallegame.Constants.LINE_SEPARATOR;
import static wallegame.Constants.MAPFILE;
import static wallegame.Constants.USAGE_MAPFILE;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JApplet;
import javax.swing.SwingUtilities;

import wallegame.cityLoader.CityLoaderFromTxtFile;
import wallegame.gui.GUIController;
import wallegame.gui.MainApplet;
import wallegame.gui.MainWindow;
import wallegame.gui.MyApplet;

/**
 * @author Ricardo Eugui Fernandez
 */

/**
 * 
 * http://docs.oracle.com/javase/tutorial/deployment/applet/subclass.html
 * https://netbeans.org/kb/docs/web/applets.html
 * http://blog.chuidiang.com/2007/01/23/jframe-vs-japplet/
 * http://es.wikipedia.org/wiki/Java_applet
 * 
 */

public class Main {

	public static void main(String[] args) {
		if (args.length == 0) {
			System.err.println(BAD_PARAMS);
			System.err.println(USAGE_MAPFILE + LINE_SEPARATOR);
			System.err.println(MAPFILE);
			System.exit(1);
		} else {
			try {
				InputStream file = new FileInputStream(args[0]);
				CityLoaderFromTxtFile fileLoader = new CityLoaderFromTxtFile();
				City city = fileLoader.loadCity(file);
				RobotEngine robot = new RobotEngine(city,
						fileLoader.getInitialPlace(), Direction.NORTH);
				GUIController gc = new GUIController(robot);
				//final MainWindow mainWindow = new MainWindow(gc);
				MainApplet mainApplet = new MainApplet();
				mainApplet.init();
				//gc.startGuiController();
			} catch (FileNotFoundException e) {
				System.err
						.println(FILE_READ_ERROR + args[0] + FILE_READ_ERROR2);
				System.exit(2);
			} catch (IOException e) {
				System.err.println(FILE_FORMAT_ERROR);
				System.exit(2);
			}
		}
	}
}